package com.gs.common.util

import android.graphics.drawable.ColorDrawable
import android.view.View

object UnderlineUtil {

    // 原颜色
    class FormerColor{
        // 颜色，RGB数值
        var color: Int? = null
    }

    private val formerColor: FormerColor = FormerColor()

    /**
     * 当triggerView被聚焦时，可使changeView改变背景颜色
     * @param triggerViews 存放多个触发view
     * @param changeViews 存放多个被改变背景颜色的view
     * @param changeColorId 改变的颜色 (传入color资源id值)
     */
    fun onFocusChangeColor(triggerViews: Array<View>, changeViews: Array<View>, changeColorId: Int) {
        for ((tv,cv) in triggerViews.zip(changeViews)) {
            tv.setOnFocusChangeListener { view, hasFocus ->
                if (formerColor.color == null) {
                    val colorDrawable = cv.background as ColorDrawable
                    formerColor.color = colorDrawable.color
                }
                if (hasFocus) {
                    cv.setBackgroundColor(cv.context.getColor(changeColorId))
                } else {
                    cv.setBackgroundColor(formerColor.color!!)
                }
            }
        }
    }
}